package com.ffzl.cloud.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.ffzl.cloud.R;
import com.ffzl.cloud.app.AppActivity;
import com.ffzl.cloud.ui.Constants;
import com.hjq.toast.ToastUtils;
import com.neusoft.qrcode.core.QRCodeView;
import com.neusoft.qrcode.core.ZXingView;

import okhttp3.Call;

public class ScanActivity2 extends AppActivity implements QRCodeView.Delegate {

    private ZXingView zXingView;

    public static void start(Context context) {
        Intent intent = new Intent(context, ScanActivity2.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scan;
    }

    @Override
    protected void initView() {
        zXingView = findViewById(R.id.zxingview);
        zXingView.setDelegate(this);
        zXingView.startCamera();
        zXingView.startSpotAndShowRect();
    }

    @Override
    protected void initData() {


    }


    @Override
    public void onScanQRCodeSuccess(String result) {
        Intent intent = new Intent();
        intent.putExtra("scanResult", result);
        setResult(Constants.SCAN_ACTIVITY_RESULT_CODE, intent);
        finish();
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {
        String tipText = zXingView.getScanBoxView().getTipText();
        String ambientBrightnessTip = "\n环境过暗，请打开闪光灯";
        if (isDark) {
            zXingView.openFlashlight();
            if (!tipText.contains(ambientBrightnessTip)) {

                zXingView.getScanBoxView().setTipText(tipText + ambientBrightnessTip);
            }
        } else {
            if (tipText.contains(ambientBrightnessTip)) {
                tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
                zXingView.getScanBoxView().setTipText(tipText);
            }
        }
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        ToastUtils.show("打开相机出错~");
    }


    @Override
    public void onStart(Call call) {
        super.onStart(call);
    }

    @Override
    protected void onDestroy() {
        zXingView.onDestroy();
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        zXingView.stopCamera();
        super.onStop();
    }


}